package vue.edition;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.BoxLayout;

import java.awt.Window.Type;
import java.awt.Dimension;

import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.FlowLayout;

import javax.swing.JTextArea;
import javax.swing.JButton;

import controler.Lieu;
import controler.News;
import controler.Poi;
import controler.consultation.ConsultLieu;
import controler.edition.EditLieu;
import controler.edition.EditNews;
import controler.edition.EditPoi;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.SwingConstants;

public class ModificationPopup extends JFrame {

	private JPanel contentPane;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextArea textArea_1;
	private JTextArea textArea_2;
	private JComboBox comboBox_1;
	private News news = null;
	private Poi poi = null;
	private Lieu lieu = null;
	private JPanel panel_6;
	private JPanel panel_7;
	private JPanel panel_8;

	/**
	 * Create the frame.
	 */
	public ModificationPopup() {
		initBase();
	}

	public void initBase() {
		setAlwaysOnTop(true);
		setType(Type.POPUP);
		setTitle("Modifier");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(15, 15, 15, 15));
		setContentPane(contentPane);
		setBounds(100, 100, 500, 300);
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
		
		panel_6 = new JPanel();
		contentPane.add(panel_6);
		panel_6.setLayout(new BorderLayout(0, 0));
		
		panel_8 = new JPanel();
		panel_6.add(panel_8, BorderLayout.NORTH);
		
		panel_7 = new JPanel();
		contentPane.add(panel_7);
		
	}
	
	public void initNews(News news) {
		EditNews edit = new EditNews();
		this.news = edit.recupNews(news.getId());

		setBounds(100, 100, 300, 300);
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
		JPanel panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		contentPane.add(panel);
		
		JLabel lblLibell = new JLabel("Libell\u00E9 :");
		panel.add(lblLibell);
		
		textField_1 = new JTextField(this.news.getLibelle());
		panel.add(textField_1);
		textField_1.setColumns(10);
		
		JPanel panel_1 = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) panel_1.getLayout();
		flowLayout_1.setAlignment(FlowLayout.LEFT);
		contentPane.add(panel_1);
		
		JLabel lblDescription = new JLabel("Description :");
		panel_1.add(lblDescription);
		
		JPanel panel_2 = new JPanel();
		contentPane.add(panel_2);
		panel_2.setLayout(new BoxLayout(panel_2, BoxLayout.X_AXIS));
		
		textArea_1 = new JTextArea(this.news.getDescription());
		panel_2.add(textArea_1);
		textArea_1.setBorder(new LineBorder(new Color(192, 192, 192)));
		
		JPanel panel_3 = new JPanel();
		FlowLayout flowLayout_3 = (FlowLayout) panel_3.getLayout();
		flowLayout_3.setAlignment(FlowLayout.LEFT);
		contentPane.add(panel_3);
		
		JLabel lblLien = new JLabel("Lien :  ");
		panel_3.add(lblLien);
		
		textField_2 = new JTextField(this.news.getLien());
		panel_3.add(textField_2);
		textField_2.setColumns(10);
		
		JPanel panel_4 = new JPanel();
		FlowLayout flowLayout_4 = (FlowLayout) panel_4.getLayout();
		flowLayout_4.setAlignment(FlowLayout.LEFT);
		contentPane.add(panel_4);
		
		JLabel lblType = new JLabel("Type :");
		panel_4.add(lblType);
		
		textField_3 = new JTextField(this.news.getType());
		panel_4.add(textField_3);
		textField_3.setColumns(10);
		
		JPanel panel_5 = new JPanel();
		FlowLayout flowLayout_5 = (FlowLayout) panel_5.getLayout();
		contentPane.add(panel_5);
		
		JButton btnNewButton = new JButton("Modifier");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new EditNews().update(ModificationPopup.this.news.getId(), textField_1.getText(), textArea_1.getText(), textField_2.getText(), textField_3.getText());
				dispose();
			}
		});
		panel_5.add(btnNewButton);
	}

	public void initLieu(Lieu lieu) {
		EditLieu edit = new EditLieu();
		this.lieu = edit.recupLieu(lieu.getId());

		setBounds(100, 100, 300, 300);
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
		JPanel panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		contentPane.add(panel);
		
		JLabel lblLibell_1 = new JLabel("Libell\u00E9 :");
		panel.add(lblLibell_1);
		
		textField_1 = new JTextField(this.lieu.getLibelle());
		panel.add(textField_1);
		textField_1.setColumns(10);
		
		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1);
		panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.Y_AXIS));
		
		JPanel panel_4 = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) panel_4.getLayout();
		flowLayout_1.setAlignment(FlowLayout.LEFT);
		panel_1.add(panel_4);
		
		JLabel lblDescription_1 = new JLabel("Description :");
		panel_4.add(lblDescription_1);
		
		JPanel panel_5 = new JPanel();
		panel_1.add(panel_5);
		panel_5.setLayout(new BoxLayout(panel_5, BoxLayout.X_AXIS));
		
		textArea_1 = new JTextArea(this.lieu.getDescription());
		panel_5.add(textArea_1);
		
		JPanel panel_2 = new JPanel();
		FlowLayout flowLayout_2 = (FlowLayout) panel_2.getLayout();
		flowLayout_2.setAlignment(FlowLayout.LEFT);
		contentPane.add(panel_2);
		
		JLabel lblImage = new JLabel("Image :");
		panel_2.add(lblImage);
		
		textField_2 = new JTextField(this.lieu.getImage());
		panel_2.add(textField_2);
		textField_2.setColumns(10);
		
		JButton button = new JButton("...");
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				// Demonstrate "Open" dialog:
				int rVal = fileChooser.showOpenDialog(ModificationPopup.this);
				if (rVal == JFileChooser.APPROVE_OPTION) {
					textField_2.setText(fileChooser.getCurrentDirectory().toString() + "\\" + fileChooser.getSelectedFile().getName());
				}
				if (rVal == JFileChooser.CANCEL_OPTION) {
					textField_2.setText("");
				}
			}
		});
		panel_2.add(button);
		
		JPanel panel_3 = new JPanel();
		contentPane.add(panel_3);
		
		JButton btnModifier = new JButton("Modifier");
		btnModifier.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new EditLieu().update(ModificationPopup.this.lieu.getId(), textField_1.getText(), textArea_1.getText(), textField_2.getText());
				dispose();
			}
		});
		panel_3.add(btnModifier);
	}

	
	public void initPoi(Poi poi) {
		EditPoi edit = new EditPoi();
		this.poi = edit.recupPoi(poi.getId());

		setBounds(100, 100, 300, 300);
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
		JPanel panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		contentPane.add(panel);
		
		JLabel lblLibell = new JLabel("Libell\u00E9 :");
		panel.add(lblLibell);
		
		textField_1 = new JTextField(this.poi.getLibelle());
		panel.add(textField_1);
		textField_1.setColumns(10);
		
		JPanel panel_1 = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) panel_1.getLayout();
		flowLayout_1.setAlignment(FlowLayout.LEFT);
		contentPane.add(panel_1);
		
		JLabel lblDescription = new JLabel("Description :");
		panel_1.add(lblDescription);
		
		JPanel panel_2 = new JPanel();
		contentPane.add(panel_2);
		panel_2.setLayout(new BoxLayout(panel_2, BoxLayout.X_AXIS));
		
		textArea_1 = new JTextArea(this.poi.getDescription());
		panel_2.add(textArea_1);
		textArea_1.setBorder(new LineBorder(new Color(192, 192, 192)));
		
		JPanel panel_3 = new JPanel();
		FlowLayout flowLayout_3 = (FlowLayout) panel_3.getLayout();
		flowLayout_3.setAlignment(FlowLayout.LEFT);
		contentPane.add(panel_3);
		
		JLabel lblLien = new JLabel("PosX :  ");
		panel_3.add(lblLien);
		
		textField_2 = new JTextField(this.poi.getPosX());
		panel_3.add(textField_2);
		textField_2.setColumns(10);
		
		JPanel panel_4 = new JPanel();
		FlowLayout flowLayout_4 = (FlowLayout) panel_4.getLayout();
		flowLayout_4.setAlignment(FlowLayout.LEFT);
		contentPane.add(panel_4);
		
		JLabel lblType = new JLabel("PosY :");
		panel_4.add(lblType);
		
		textField_3 = new JTextField(this.poi.getPosY());
		panel_4.add(textField_3);
		textField_3.setColumns(10);
		
		
		
		
		
		JPanel panel_6 = new JPanel();
		contentPane.add(panel_6);
		panel_2.setLayout(new BoxLayout(panel_2, BoxLayout.X_AXIS));
		
		JLabel lblDescriptionS = new JLabel("Description Simple :");
		panel_6.add(lblDescriptionS);
		
		textArea_2 = new JTextArea(this.poi.getDescriptionSimple());
		panel_6.add(textArea_2);
		textArea_2.setBorder(new LineBorder(new Color(192, 192, 192)));
		
		JPanel panel_7 = new JPanel();
		contentPane.add(panel_7);
		
		comboBox_1 = new JComboBox<>();
		new ConsultLieu().listerLieu(comboBox_1);
		panel_7.add(comboBox_1);
		
		JPanel panel_8 = new JPanel();
		FlowLayout flowLayout_10 = (FlowLayout) panel_8.getLayout();
		flowLayout_10.setAlignment(FlowLayout.LEFT);
		contentPane.add(panel_8);
		
		JLabel lblImage = new JLabel("Image :");
		panel_8.add(lblImage);
		
		textField_5 = new JTextField(this.poi.getImage());
		textField_5.setColumns(10);
		
		JButton button = new JButton("...");
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				// Demonstrate "Open" dialog:
				int rVal = fileChooser.showOpenDialog(ModificationPopup.this);
				if (rVal == JFileChooser.APPROVE_OPTION) {
					textField_5.setText(fileChooser.getCurrentDirectory().toString() + "\\" + fileChooser.getSelectedFile().getName());
				}
				if (rVal == JFileChooser.CANCEL_OPTION) {
					textField_5.setText("");
				}
			}
		});
		panel_8.add(button);
		
		JPanel panel_10 = new JPanel();
		FlowLayout flowLayout_8 = (FlowLayout) panel_10.getLayout();
		contentPane.add(panel_10);
		
		JButton btnNewButton = new JButton("Modifier");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String[] split = comboBox_1.getSelectedItem().toString().split(" - ");
				int id = Integer.parseInt(split[0]);	
				
				new EditPoi().update(ModificationPopup.this.poi.getId(), textField_1.getText(), textArea_1.getText(),textArea_2.getText(), Integer.parseInt(textField_2.getText()), Integer.parseInt(textField_3.getText()),  textField_5.getText(), id);
				dispose();
			}
		});
		panel_8.add(btnNewButton);
	}
	
}

